package DAO;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class InvoicedDao {
  public InvoicedDao() {
  }

  public boolean creaInv(int OwnerNumber, Date inicialDia,
                         Date esperadaPaga, Date pago, String formaPago,
                         float totalPagado, String estado) {
    int InvoicedNum;
    Conexion conexion = new Conexion();
    boolean creado = false;
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;
      ResultSet rsa=null;
      int mayor=-1;
      
      ps =
          conex.prepareStatement("select max (treatmentnumber)  from treatment");
      rsa = ps.executeQuery();
      if (rsa != null) {
        while (rsa.next()) {
          mayor = rsa.getInt(1);
        }
      } else
        InvoicedNum = 1;

      
      ps =
          conex.prepareStatement("INSERT INTO INVOICED (INVOICEDNUMBER, INITIALDATE, EXPECTEDDATE, PAYDATE, TOTAL, STATUS,OWNERNUMBER,TYPEPAY) VALUES (?,?,?,?,?,?,?,?)");
      InvoicedNum=mayor+1;
      ps.setInt(1,InvoicedNum);
      ps.setDate(2, inicialDia);
      ps.setDate(3, esperadaPaga);
      ps.setDate(4, pago);
      ps.setFloat(5, totalPagado);
      ps.setString(6, estado);
      ps.setInt(7, OwnerNumber);
      ps.setString(8, formaPago);


      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);
      creado = true;
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return creado;
  }
  
  public boolean modificaDiaInicial(int numInvo, Date inicio) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE invoiced set initialdate = ?  where invoicednumber = ?");

      ps.setDate(1, inicio);

      ps.setInt(2, numInvo);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaFechaEsperada(int numtreIn, Date esperada) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE invoiced set expecteddate = ?  where invoicednumber = ?");

      ps.setDate(1, esperada);

      ps.setInt(2, numtreIn);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaFechaPago(int numIn, Date pago) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;


  System.out.println("Estamos en DAO " + numIn);
      ps =
          conex.prepareStatement("UPDATE invoiced set paydate = ?  where invoicednumber = ?");

      ps.setDate(1, pago);

      ps.setInt(2, numIn);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaTotal(int numInv, float costo) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE invoiced set total = ?  where invoicednumber = ?");

      ps.setFloat(1, costo);

      ps.setInt(2, numInv);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaStatus(int numInv,String estado) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE invoiced set status = ?  where invoicednumber = ?");

      ps.setString(1, estado);

      ps.setInt(2, numInv);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaOwnerNum(int numInv, int ownerNum) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE invoiced set ownernumber = ?  where invoicednumber = ?");

      ps.setInt(1, ownerNum);

      ps.setInt(2, numInv);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaTypePay(int numIn, String typepay) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE invoiced set typepay = ?  where invoicednumber = ?");

      ps.setString(1, typepay);

      ps.setInt(2, numIn);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean eliminarFactura(int FACNum) {

    Conexion conexion = new Conexion();
    boolean eliminado = false;
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ps = conex.prepareStatement("DELETE FROM INVOICED where INVOICEDNUMBER = ?");
      ps.setInt(1, FACNum);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que borro fueron" + numeroFilas);
      eliminado = true;
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return eliminado;
  }

  public Object[][] DatosFacturaTabla() {
    Conexion conexion = new Conexion();
    Connection conex;
    Object[][] datos = new Object[100][100];

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ps = conex.prepareStatement("select * FROM Invoiced");     
      int i = 0;
      int j = 0;

      rs = ps.executeQuery();
      System.out.println(rs + " es el resultado de la consulta !");
      
      datos[i][j]= "INVOICED NUMBER";
      j++;
      datos[i][j]= "INICIAL DATE";
      j++;
      datos[i][j]="EXPECTED DAY";
      j++;
      datos[i][j]="PAY DAY";
      j++;
      datos[i][j]="TOTAL";
      j++;
      datos[i][j]="STATUS" ;
      j++;
      datos[i][j]="OWNER NUMBER";
      j++;
      datos[i][j]="TYPE PAY";
      i++;
      j=0;
      
      while (rs.next()) {        
        datos[i][j] = rs.getInt(1);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getDate(2);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getDate(3);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getDate(4);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getFloat(5);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getString(6);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getInt(7);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getString(8);
        System.out.print(datos[i][j] + " ");
        System.out.println("FIN!! ");
        i++;
        j=0;
      }
      
      rs.close();
      ps.close();
      conex.close();      
    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
    }
    return datos;
  }
}
